import { Component, OnInit } from '@angular/core';
import { NoticeService } from '../../services/notice/notice.service';
import { listModel, noticeModel } from './notice.model';
import { NzModalService } from 'ng-zorro-antd';
import { NzMessageService } from 'ng-zorro-antd';
import { Utils } from 'src/app/common/helper/util-helper';

@Component({
  selector: 'app-notice',
  templateUrl: './notice.component.html',
  styleUrls: ['./notice.component.scss']
})
export class NoticeComponent implements OnInit {

  constructor(
    private noticeService: NoticeService,
    private modalService: NzModalService,
    public message: NzMessageService
  ) { }

  public findJson = new listModel();
  public notices: any = [];
  public nzPageIndex: number = 1;
  public nzTotal: number = 0;
  public isAddVisible: boolean = false;
  public departments: any = [];
  public addNoticeModel = new noticeModel();
  public isDetailsVisible: any = false;
  public appoint: any = {};
  public userInfo: any = JSON.parse(window.localStorage.getItem('APDInfo'));
  public statusShow: boolean = true;
  public status = {
    // 当前页面所需要的权限
    pagePermissions: [
      // 发布公告
      'APJ.System.Notices',
    ],
    // 当前页面权限
    pageAuthorty: {
      SystemNotices: null,
    },
  }



  /**
   * 初始化
   */
  public async init() {

    // 初始化企业级权限匹配
    this.status.pageAuthorty = Utils.grantedPermissions(
      this.status.pagePermissions,
      this.status.pageAuthorty,
      true
    )
    if (this.userInfo.roleCode === 'Admin') {
      const result = await this.noticeService.getAdminNotices(this.findJson);
      this.notices = result.result.items;
      this.nzTotal = result.result.totalCount;
      this.statusShow = false;
      this.getShow(this.notices);
    } else {
      const result = await this.noticeService.getNotices(this.findJson);
      this.notices = result.result.items;
      this.nzTotal = result.result.totalCount;
      this.statusShow = true;
      this.getStyle(this.notices);
    }
  }

  public getStyle(data) {
    data.forEach(element => {
      if (element.status === 1) {
        element.style = {
          'color': '#a3a3a3'
        }
      } else {
        element.style = {
          'color': 'red'
        }
      }
    });
  }

  public getShow(data) {
    data.forEach(element => {
      if (element.status === 1) {
        element.show = true;
      } else {
        element.show = false;
      }
    });
  }

  public async pageChange(index) {
    this.findJson.skipCount = index - 1;
    this.init();
  }

  public async getDepartments() {
    const result = await this.noticeService.getOrganizations({ MaxResultCount: 1000, SkipCount: 0 });
    this.departments = result.result.items;
    console.log(result)
  }

  public async search() {
    this.findJson.skipCount = 0;
    this.init();
  }

  /**
   * 状态改变
   * @param item 
   */
  public async statusChange(item) {
    const result = await this.noticeService.showOrHide(item.id);
    console.log(result)
  }

  /**
   * 发布
   */
  public release() {
    this.isAddVisible = true;
  }

  public async handleAddOk() {
    console.log(this.addNoticeModel)

    if (!this.addNoticeModel.title) {
      this.message.warning('标题不能为空')
      return;
    } else if (!this.addNoticeModel.content) {
      this.message.warning('内容不能为空')
      return;
    } else if (!this.addNoticeModel.organizationUnitId) {
      this.message.warning('请选择推送部门')
      return;
    } else {
      let res = await this.noticeService.addNotice(this.addNoticeModel);
      if (res.success) {
        this.message.success('发布成功')
      } else {
        this.message.warning(res.error.message);
      }
      this.init();
    }
    this.isAddVisible = false;
  }

  public handleCancel() {
    this.isAddVisible = false;
    this.isDetailsVisible = false;
  }

  /**
   * 详情
   * @param item 
   */
  public async details(item) {
    const result = await this.noticeService.noticeDetails(item.id);
    this.appoint = result.result;
    this.isDetailsVisible = true;
    // 标记为已读
    await this.noticeService.markRead([item.id]);
    this.init();
  }

  /**
   * 删除公告
   * @param id 
   */
  public async deleteNotice(id) {
    this.modalService.error({
      nzTitle: '确认要删除该项吗?',
      nzContent: '<b style="color: red;">删除后不能恢复，请谨慎操作。</b>',
      nzOkText: '确定',
      nzOkType: 'danger',
      nzOnOk: async () => {
        const json = [id];
        if (this.userInfo.roleCode === 'Admin') {
          await this.noticeService.deleteAdminNotice(json);
          this.init();
        } else {
          await this.noticeService.deleteNotices(json);
          this.init();
        }
      },
      nzCancelText: '取消',
      nzOnCancel: () => { }
    });
  }

  ngOnInit() {
    this.init();
    this.getDepartments();
  }

}
